package cn.edu.fzu.homemaking.controller;

import cn.edu.fzu.homemaking.api.OrderApi;
import cn.edu.fzu.homemaking.common.PageVO;
import cn.edu.fzu.homemaking.common.SessionConstant;
import cn.edu.fzu.homemaking.domain.entity.OrderScore;
import cn.edu.fzu.homemaking.domain.entity.User;
import cn.edu.fzu.homemaking.domain.enums.CommentStatusEnum;
import cn.edu.fzu.homemaking.domain.vo.OrderScoreVo;
import cn.edu.fzu.homemaking.service.OrderScoreService;
import cn.edu.fzu.homemaking.web.Result;
import cn.edu.fzu.homemaking.web.RetCode;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import io.swagger.annotations.*;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Author jiebin
 * @Date 2023/4/3
 */
@RestController
@RequestMapping("/api/user")
@Api(tags = "用户订单评价管理")
public class OrderScoreController {
    @Resource
    private OrderScoreService orderScoreService;

    @Resource
    private OrderApi orderApi;

    @PostMapping("/auth/comment/insert")
    @ApiOperation(value = "添加订单评价", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orderId", value = "订单号", required = true),
            @ApiImplicitParam(name = "companyId", value = "公司id", required = true),
            @ApiImplicitParam(name = "score", value = "评分", required = true),
            @ApiImplicitParam(name = "comments", value = "评价",required = true),
    })
    @ApiResponse(code = 200, message = "", response = Result.class)
    public Result insert(String orderId, Long companyId, Integer score, String comments, HttpServletRequest request){
        User user = (User)request.getSession().getAttribute(SessionConstant.ACCOUNT);
        orderApi.updateCommentStatus(orderId, CommentStatusEnum.YES);
        orderScoreService.insert(orderId, companyId, user.getId(), score, comments);
        return new Result(RetCode.SUCCESS,"评价发布成功");
    }

    @GetMapping("/comment/list")
    @ApiOperation(value = "获取评价列表（不填公司id，默认获取用户自己的评价）", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "companyId", value = "公司id"),
            @ApiImplicitParam(name = "startTime", value = "开始时间"),
            @ApiImplicitParam(name = "endTime", value = "结束时间"),
            @ApiImplicitParam(name = "pageNum", required = true),
            @ApiImplicitParam(name = "pageSize", required = true)
    })
    @ApiResponse(code = 200, message = "", response = Result.class)
    public Result list(Long companyId, String startTime, String endTime,
                       Integer pageNum, Integer pageSize, HttpServletRequest request){
        try(Page<OrderScore> page = PageHelper.startPage(pageNum, pageSize)){
            List<OrderScoreVo> list =  orderScoreService.list(companyId, null, startTime, endTime);
            return new Result(new PageVO<>(page.getTotal(),list));
        }
    }

    @GetMapping("/comment/selectByOrderId")
    @ApiOperation(value = "查询订单评价", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orderId", value = "订单号")
    })
    @ApiResponse(code = 200, message = "", response = Result.class)
    public Result selectByOrderId(String orderId){
        OrderScoreVo orderScore = orderScoreService.selectByOrderId(orderId);
        return new Result(orderScore);
    }
}
